package com.fy.tt.service.impl;

import com.fy.tt.action.TemplateTool;
import com.fy.tt.dao.SentenceDao;
import com.fy.tt.entity.Sentence;
import com.fy.tt.entity.query.SentenceQuery;
import com.fy.tt.entity.vo.SentenceVo;
import com.fy.tt.service.SentenceService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

public class SentenceServiceImpl implements SentenceService {
    Connection dbConnection = null;
    SentenceDao sentenceDao = null;

    {
        try {
            dbConnection = TemplateTool.getDBConnection();
            sentenceDao = TemplateTool.getBean(SentenceDao.class);
        } catch (Exception e) {
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }

    @Override
    public List<SentenceVo> getAllSentences() {
        List<SentenceVo> sentences = null;
        try {
            sentences = sentenceDao.getAllSentences(dbConnection);
        } catch (Exception e) {

        }
        return sentences;
    }

    @Override
    public int getTotalSentence(SentenceQuery sentenceQuery) {
        int count = -1;
        try {
            count = sentenceDao.getTotalSentence(dbConnection, sentenceQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int addSentence(Sentence sentence) {
        int i = -1;
        try {
            sentenceDao.save(dbConnection, sentence);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteSentence(int id) {
        int i = -1;
        try {
            i = sentenceDao.deleteSentence(dbConnection, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateSentence(Sentence sentence) {
        int i = -1;
        try {
            i = (int) sentenceDao.updateIgnoreNull(dbConnection, sentence);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<Sentence> getPageSentences(int pageSize, int current) {
        List<Sentence> sentences = null;
        try {
            sentences = sentenceDao.getPageSentences(dbConnection, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sentences;
    }

    @Override
    public List<SentenceVo> getConditionPageSentences(SentenceQuery sentenceQuery, int pageSize, int current) {
        List<SentenceVo> sentences = null;
        try {
            sentences = sentenceDao.getConditionPageSentences(dbConnection, sentenceQuery, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sentences;
    }

    @Override
    public List<SentenceVo> getLastSaveSentence() {
        List<SentenceVo> allSentences = this.getAllSentences();
        List<SentenceVo> collect = allSentences.stream().sorted((a, b) -> {
            return (int) (b.getUpdateTime().getTime() - a.getUpdateTime().getTime());
        }).limit(5L).collect(Collectors.toList());
        return collect;
    }
}
